package com.wzd.imservice.service.impl;

import com.wzd.imservice.entity.Message;
import com.wzd.imservice.entity.query.HistoryMessageQuery;
import com.wzd.imservice.enums.ChatTypeEnum;
import com.wzd.imservice.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: e_learning
 * @Package: com.wzd.imservice.service.impl
 * @ClassName: MessageServiceImpl
 * @Author: lofxve
 * @Description:
 * @Date: 2021/4/20 10:35
 * @Version: 1.0
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean save(Message message) {
        // 使用mongo生成的id
        message.setId(null);
        // 更改发送者
        message.setMine(true);
        Message save = mongoTemplate.save(message);
        if (save.getId() != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Message> getHistoryMessage(HistoryMessageQuery historyMessageQuery) {
        ArrayList<Message> messages = new ArrayList<>();
        // 群消息
        if (historyMessageQuery.getType().equals(ChatTypeEnum.getNameByKey(1))) {
            // 查询条件
            Query toid = new Query(Criteria.where("toid").is(historyMessageQuery.getToId()))
                    // 时间倒叙
                    .with(Sort.by(Sort.Order.desc("timestamp")));
            // 开始查询
            List<Message> groupMessages = mongoTemplate.find(toid, Message.class);
            for (Message groupMessage : groupMessages) {
                // 如果是自己发的消息则在右侧显示
                if (groupMessage.getFromid().equals(historyMessageQuery.getFromId())) {
                    groupMessage.setMine(true);
                } else {
                    groupMessage.setMine(false);
                }
                messages.add(groupMessage);
            }
        } else {
            // 查询条件
            Criteria criteriafromid = Criteria.where("toid").is(historyMessageQuery.getToId())
                    .and("fromid").is(historyMessageQuery.getFromId());
            Criteria criteriatoid = Criteria.where("toid").is(historyMessageQuery.getFromId())
                    .and("fromid").is(historyMessageQuery.getToId());
            Criteria criteria = new Criteria().orOperator(criteriafromid, criteriatoid);

            Query toid = new Query(criteria)
                    // 时间倒叙
                    .with(Sort.by(Sort.Order.desc("timestamp")));
            // 开始查询
            List<Message> groupMessages = mongoTemplate.find(toid, Message.class);
            for (Message groupMessage : groupMessages) {
                // 如果是自己发的消息则在右侧显示
                if (groupMessage.getFromid().equals(historyMessageQuery.getFromId())) {
                    groupMessage.setMine(true);
                } else {
                    groupMessage.setMine(false);
                }
                messages.add(groupMessage);
            }
        }
        return messages;
    }
}
